#ifndef __PS2_HANDLE_H
#define __PS2_HANDLE_H
#include "main.h"


/**
 * *All of the code from the Internet*
 * Reorganize: Vaine0
 * Date: 2021/3/10
 **/


#define CPU_FREQUENCY_MHZ 168 // SYSCLK //  78


#define DI    HAL_GPIO_ReadPin(PS2_Handle_DI_GPIO_Port, PS2_Handle_DI_Pin)

#define DO_H  HAL_GPIO_WritePin(PS2_Handle_DO_GPIO_Port, PS2_Handle_DO_Pin, GPIO_PIN_SET)
#define DO_L  HAL_GPIO_WritePin(PS2_Handle_DO_GPIO_Port, PS2_Handle_DO_Pin, GPIO_PIN_RESET)

#define CS_H  HAL_GPIO_WritePin(PS2_Handle_CS_GPIO_Port, PS2_Handle_CS_Pin, GPIO_PIN_SET)
#define CS_L  HAL_GPIO_WritePin(PS2_Handle_CS_GPIO_Port, PS2_Handle_CS_Pin, GPIO_PIN_RESET)

#define CLK_H HAL_GPIO_WritePin(PS2_Handle_CLK_GPIO_Port, PS2_Handle_CLK_Pin, GPIO_PIN_SET)
#define CLK_L HAL_GPIO_WritePin(PS2_Handle_CLK_GPIO_Port, PS2_Handle_CLK_Pin, GPIO_PIN_RESET)


// These are our button constants
#define PSB_SELECT      1
#define PSB_L3          2
#define PSB_R3          3
#define PSB_START       4
#define PSB_PAD_UP      5
#define PSB_PAD_RIGHT   6
#define PSB_PAD_DOWN    7
#define PSB_PAD_LEFT    8
#define PSB_L2          9
#define PSB_R2          10
#define PSB_L1          11
#define PSB_R1          12
#define PSB_GREEN       13
#define PSB_RED         14
#define PSB_BLUE        15
#define PSB_PINK        16

#define PSB_TRIANGLE    13
#define PSB_CIRCLE      14
#define PSB_CROSS       15
#define PSB_SQUARE      16

// These are stick values
#define PSS_RX 5
#define PSS_RY 6
#define PSS_LX 7
#define PSS_LY 8


extern uint8_t PS2_Handle_Data[9];
extern uint16_t MASK[20];
extern uint16_t Handkey;

// void PS2_Handle_test(void);
void delay_us(uint32_t nus);
void PS2_Init(void);
uint8_t PS2_RedLight(void);
uint8_t PS2_ReadByte(uint8_t command);
void PS2_ReadData(uint8_t *command, uint8_t size);
uint8_t PS2_DataKey(void);
uint8_t PS2_AnologData(uint8_t button);
void PS2_ClearData(void);
void PS2_Vibration(uint8_t motor1, uint8_t motor2);


#endif
